package com.tyao.note.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tyao.framework.core.JsonResult;
import com.tyao.note.model.BFolder;
import com.tyao.note.service.FolderService;
import com.tyao.note.service.exception.FolderNameAlreadyExistException;

@Controller
public class FolderController extends BaseController {

	@Resource
	private FolderService folderService;

	@RequestMapping("/note/folderList")
	public String index(HttpServletRequest request) {
		HttpSession session = request.getSession();
		List<BFolder> folders = folderService.getAllFolderByUserId(getId(session));
		request.setAttribute("folders", folders);
		BFolder defaultFolder=folderService.getDefaultFolder();
		request.setAttribute("defaultFolder",defaultFolder );
		return "/folder-list";
	}
	
	@RequestMapping({"/note/cutFolder","/note/copyFolder"})
	@ResponseBody
	public JsonResult cutFolder(Long id,Long folderParentId,Boolean isCut) {
		JsonResult jsonResult = new JsonResult();
		try {
			folderService.cutOrCopy(id, folderParentId,isCut);
			jsonResult.setSuccess(true);
			jsonResult.setMsg("复制成功");
		} catch (FolderNameAlreadyExistException e) {
			jsonResult.setSuccess(false);
			jsonResult.setMsg(e.getMessage());
		}
		return jsonResult;
	}

	@RequestMapping("/note/renameFolderInfo")
	@ResponseBody
	public JsonResult renameFolderInfo(Long id, String folderName) {
		JsonResult jsonResult = new JsonResult();
		try {
			folderService.updateFolderInfo(id, folderName);
			jsonResult.setSuccess(true);
			jsonResult.setMsg("重命名成功");
		} catch (FolderNameAlreadyExistException e) {
			jsonResult.setSuccess(false);
			jsonResult.setMsg(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 删除文件夹
	 * 
	 * @param id
	 * @param deleteBatch是否删除连同子文件夹一块删除
	 * @return
	 */
	@RequestMapping("/note/removeFolders")
	@ResponseBody
	public JsonResult removeFolders(Long id, Boolean deleteBatch) {
		JsonResult jsonResult = new JsonResult();
		folderService.removeFolder(id, deleteBatch);
		jsonResult.setSuccess(true);
		jsonResult.setMsg("删除成功");
		return jsonResult;
	}

	@RequestMapping("/note/createFolder")
	@ResponseBody
	public JsonResult createFolder(String folderName, Long id, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		try {
			// 创建其他文件夹
			Long primaryKey = folderService.insertFolder(folderName, id, getId(session));
			BFolder bFolder = folderService.getFolderById(primaryKey);
			jsonResult.setSuccess(true);
			jsonResult.setMsg("文件夹创建成功");
			jsonResult.setData(bFolder);
		} catch (Exception e) {
			jsonResult.setSuccess(false);
			jsonResult.setMsg(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 初次访问页面发送请求，无条件创建根目录
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping("/note/createRootFolder")
	@ResponseBody
	public JsonResult createRootFolder(HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		try {
			// 创建根目录
			folderService.insertFolder(null, true, getId(session));
			jsonResult.setSuccess(true);
			jsonResult.setMsg("根目录创建成功");
		} catch (Exception e) {
			jsonResult.setSuccess(false);
			jsonResult.setMsg("根目录已经存在");
		}
		List<BFolder> folders = folderService.getAllFolderByUserId(getId(session));
		jsonResult.setData(folders);
		return jsonResult;
	}
	/**
	 * 获取该用户下的所有文件夹
	 * @param session
	 * @return
	 */
	@RequestMapping("/note/getFolders")
	@ResponseBody
	public JsonResult getFolders(HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		List<BFolder> folders = folderService.getAllFolderByUserId(getId(session));
		jsonResult.setData(folders);
		return jsonResult;
	}

}
